<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Exchanging currencies - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.currency.exchange.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.currency.exchange.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.currency.calculation.html">Calculating with currencies</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.currency.html">Zend_Currency</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.currency.additional.html">Additional informations on Zend_Currency</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.currency.exchange" class="section"><div class="info"><h1 class="title">Exchanging currencies</h1></div>
    

    <p class="para">
        Within the previous section we discussed currency calculations. But as you can imaging
        calculating currencies does often mean to calculate different currencies from different
        countries.
    </p>

    <p class="para">
        In this case the currencies have to be exchanged so that both use the same currency.
        Within real live this information is available by banks or by daily papers. But as we
        are in web, we should use available exchange services.
        <span class="classname">Zend_Currency</span> allows their usage with a simple callback.
    </p>

    <p class="para">
        First let&#039;s write a simple exchange service.
    </p>

    <pre class="programlisting brush: php">
class SimpleExchange implements Zend_Currency_CurrencyInterface
{
    public function getRate($from, $to)
    {
        if ($from !== &quot;USD&quot;) {
            throw new Exception(&#039;We can only exchange USD&#039;);
        }

        switch ($to) {
            case &#039;EUR&#039;:
                return 2;
            case &#039;JPE&#039;:
                return 0.7;
       }

       throw new Exception(&#039;Unable to exchange $to&#039;);
    }
}
</pre>


    <p class="para">
        We have now created a manual exchange service. It will not fit the real live, but it
        shows you how currency exchange works.
    </p>

    <p class="para">
        Your exchange class must implement the
        <span class="classname">Zend_Currency_CurrencyInterface</span> interface. This interface
        requires the single method  <span class="methodname">getRate()</span> to be implemented. This
        method has two parameters it will receive. Both are the short names for the given
        currencies. <span class="classname">Zend_Currency</span> on the other side needs the exchange
        rate to be returned.
    </p>

    <p class="para">
        In a living exchange class you would probably ask the service provider for the correct
        exchange rates. For our example the manual rate will be ok.
    </p>

    <p class="para">
        Now we will simply attach our exchange class with <span class="classname">Zend_Currency</span>.
        There are two ways to do this. Eigher by attaching a instance of the Exchange class, or
        by simply giving a string with the classname.
    </p>

    <pre class="programlisting brush: php">
$currency = new Zend_Currency(
    array(
        &#039;value&#039;    =&gt; 1000,
        &#039;currency&#039; =&gt; &#039;EUR&#039;,
    )
);

$service  = new SimpleExchange();

// attach the exchange service
$currency-&gt;setService($service);

$currency2 = new Zend_Currency(
    array(
        &#039;value&#039;    =&gt; 1000,
        &#039;currency&#039; =&gt; &#039;USD&#039;,
    )
);

print $currency-&gt;add($currency2);
</pre>


    <p class="para">
        The above example will return &#039;$ 3.000&#039; because the 1.000 <acronym class="acronym">USD</acronym> will be
        converted by a rate of 2 to 2.000 <acronym class="acronym">EUR</acronym>.
    </p>

    <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Calculation without exchange service</b><br /></span>
        

        <p class="para">
            When you try to calculate two currency objects which do not use the same currency
            and have no exchange service attached, you will get an exception. The reason is
            that <span class="classname">Zend_Currency</span> is then not able to switch between the
            different currencies.
        </p>
    </p></blockquote>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.currency.calculation.html">Calculating with currencies</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.currency.html">Zend_Currency</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.currency.additional.html">Additional informations on Zend_Currency</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.currency.html">Zend_Currency</a></li>
  <li><a href="zend.currency.introduction.html">Introduction to Zend_Currency</a></li>
  <li><a href="zend.currency.usage.html">Using Zend_Currency</a></li>
  <li><a href="zend.currency.options.html">Options for currencies</a></li>
  <li><a href="zend.currency.description.html">What makes a currency?</a></li>
  <li><a href="zend.currency.position.html">Where is the currency?</a></li>
  <li><a href="zend.currency.number.html">How does the currency look like?</a></li>
  <li><a href="zend.currency.value.html">How much is my currency?</a></li>
  <li><a href="zend.currency.calculation.html">Calculating with currencies</a></li>
  <li class="active"><a href="zend.currency.exchange.html">Exchanging currencies</a></li>
  <li><a href="zend.currency.additional.html">Additional informations on Zend_Currency</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>